﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace SiparisKontrol
{
    public enum EvrakTipleri : byte
    {
        SatisFaturasi = 0,
        AlisFaturasi = 1,
        AlisIrsaliyesi = 2,
        DepoSevk = 3,
        GelenHavale = 4,
        GidenHavale = 5
    }

    public enum HesapTipi : byte
    {
        Cari = 0,
        Banka = 2,
        Kasa = 4,
        Masraf = 5
    }

    public class Evrak
    {
        private List<HareketBilgileri> rows = new List<HareketBilgileri>();
        public DateTime Tarih { get; set; }
        public string BelgeNo { get; set; }
        public string Seri { get; set; }
        public int Sira { get; set; }
        public EvrakTipleri EvrakCinsi { get; set; }
        public string CariKodu { get; set; }
        public string SrmMerkezKodu { get; set; }
        public string PlasiyerKodu { get; set; }
        public string MikroKullanici { get; set; }
        public int GirisDepoNo { get; set; }
        public int CikisDepoNo { get; set; }
        public string Aciklama { get; set; }
        public List<HareketBilgileri> Rows { get { return rows; } }
        public string[] EvrakAciklamalari { get; set; }
        public int Vade { get; set; }
    }
    public class HareketBilgileri
    {
        public int sharID { get; set; }
        public int stokID { get; set; }
        public int Satir { get; set; }
        public string StokKodu { get; set; }
        public string StokAdi { get; set; }
        public double Miktar { get; set; }
        public double Fiyat { get; set; }
        public double Isk1Tutar { get; set; }
        public double Isk2Tutar { get; set; }
        public double Isk3Tutar { get; set; }
        public double Isk4Tutar { get; set; }
        public double Isk5Tutar { get; set; }
        public double BurutTutar { get; set; }
        public int KdvSira { get; set; }
        public double KdvOrani { get; set; }
        public double KdvTutari { get; set; }
        public int SipDbcNo { get; set; }
        public int SipRecNo { get; set; }
        public int FaturaID { get; set; }
        public string[] SeriNo { get; set; }
    }

    public class ComboItem
    {
        public string No { get; set; }
        public string Adi { get; set; }
        public override string ToString()
        {
            return Adi;
        }
    }

    public class Mikro
    {
        private string GetString()
        {
            return string.Format("Data Source={0};User Id={1};Password={2};Database={3}", Program.Session[Settings.ServerName], Program.Session[Settings.UserName], Program.Session[Settings.Password], Program.Session[Settings.DatabaseName]);
        }

        public void SerinoHareketEkle(string seriNo, string stokKodu, int sharId, DateTime garantiBaslangic, DateTime garantiBitis, DateTime alisTarihi, DateTime satisTarihi, string alisSeri, int alisSira, string satisSeri, int satisSira, double alisFiyat, double satisFiyat, string alisCari, string satisCari, SqlTransaction trn)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("if exists(select ChHar_RECno from CIHAZ_HAREKETLERI where ChHar_SeriNo= @seriNo and ChHar_StokKodu=@stokKodu and ChHar_master_recno=@sharId)");
            sb.Append("update CIHAZ_HAREKETLERI set ChHar_Spec_Rec_no=0,ChHar_iptal=0,ChHar_fileid=98,ChHar_hidden=0,ChHar_kilitli=0,ChHar_degisti=0,ChHar_checksum=0,ChHar_lastup_user=1,ChHar_lastup_date=@tarih,ChHar_special1='',ChHar_special2='',ChHar_special3='',ChHar_master_tablo=0,ChHar_master_dbcno=0 where ChHar_SeriNo=@seriNo and ChHar_StokKodu=@stokKodu and ChHar_master_recno=@sharId else ");
            sb.Append("insert into CIHAZ_HAREKETLERI (ChHar_RECid_DBCno,ChHar_RECid_RECno,ChHar_Spec_Rec_no,ChHar_iptal,ChHar_fileid,ChHar_hidden,ChHar_kilitli,ChHar_degisti,ChHar_checksum,ChHar_create_user,ChHar_create_date,ChHar_lastup_user,ChHar_lastup_date,ChHar_special1,ChHar_special2,ChHar_special3,ChHar_SeriNo,ChHar_StokKodu,ChHar_master_tablo,ChHar_master_dbcno,ChHar_master_recno) values (0,-1,0,0,98,0,0,0,0,1,@tarih,1,@tarih,'','','',@seriNo,@stokKodu,0,0,@sharId) ");
            sb.Append(";update CIHAZ_HAREKETLERI set ChHar_RECid_RECno=ChHar_RECno where ChHar_RECid_RECno=-1");

            StringBuilder sb2 = new StringBuilder();
            sb2.Append("if exists(select chz_RECno from STOK_SERINO_TANIMLARI where chz_serino= @seriNo and chz_stok_kodu=@stokKodu)");
            sb2.Append("UPDATE    STOK_SERINO_TANIMLARI SET chz_Spec_Rec_no = 0, chz_iptal = 0, chz_fileid = 94, chz_hidden = 0, chz_kilitli = 0, chz_degisti = 0, chz_checksum = 0, chz_lastup_user = 1, chz_lastup_date = @tarih, chz_special1 = '', chz_special2 = '', chz_special3 = '', chz_Tuktckodu = '', chz_GrnBasTarihi = @garantiBaslangic,chz_GrnBitTarihi = @garantiBitis, chz_aciklama1 = '', chz_aciklama2 = '', chz_aciklama3 = '', chz_al_tarih = @alisTarih, chz_al_evr_seri = @alisSeri,chz_al_evr_sira = @alisSira, chz_st_tarih = @satisTarih, chz_st_evr_seri = @satisSeri, chz_st_evr_sira = @satisSira, chz_al_fiati_ana = @alisFiyat,chz_al_fiati_alt = @alisFiyat, chz_al_fiati_orj = @alisFiyat, chz_st_fiati_ana = @satisFiyat, chz_st_fiati_alt = @satisFiyat, chz_st_fiati_orj = @satisFiyat,chz_parca_garantisi = 0, chz_parca_serino = '', chz_parca_garanti_baslangic = @garantiBaslangic, chz_parca_garanti_bitis = @garantiBitis,chz_al_cari_kodu = @alisCari, chz_st_cari_kodu = @satisCari WHERE (chz_serino = @seriNo) AND (chz_stok_kodu = @stokKodu) else ");
            sb2.Append("INSERT INTO STOK_SERINO_TANIMLARI (chz_RECid_DBCno, chz_RECid_RECno, chz_Spec_Rec_no, chz_iptal, chz_fileid, chz_hidden, chz_kilitli, chz_degisti, chz_checksum, chz_create_user,chz_create_date, chz_lastup_user, chz_lastup_date, chz_special1, chz_special2, chz_special3, chz_serino, chz_stok_kodu, chz_grup_kodu, chz_Tuktckodu,chz_GrnBasTarihi, chz_GrnBitTarihi, chz_aciklama1, chz_aciklama2, chz_aciklama3, chz_al_tarih, chz_al_evr_seri, chz_al_evr_sira, chz_al_cari_kodu, chz_st_tarih,chz_st_evr_seri, chz_st_evr_sira, chz_st_cari_kodu, chz_al_fiati_ana, chz_al_fiati_alt, chz_al_fiati_orj, chz_st_fiati_ana, chz_st_fiati_alt, chz_st_fiati_orj,chz_parca_garantisi, chz_parca_serino, chz_parca_garanti_baslangic, chz_parca_garanti_bitis) VALUES (0, - 1, 0, 0, 94, 0, 0, 0, 0, 1,@tarih, 1,@tarih, '', '', '',@seriNo,@stokKodu, '', '',@garantiBaslangic,@garantiBitis, '', '','',@alisTarih,@alisSeri,@alisSira,@alisCari,@satisTarih,@satisSeri,@satisSira,@satisCari,@alisFiyat,@alisFiyat,@alisFiyat,@satisFiyat,@satisFiyat,@satisFiyat,0,'',@garantiBaslangic,@garantiBitis) ");
            sb2.Append(";update STOK_SERINO_TANIMLARI set chz_RECid_RECno=chz_RECno where chz_RECid_RECno=-1");

            using (SqlCommand cmd = new SqlCommand(sb.ToString(), trn.Connection))
            {
                cmd.Transaction = trn;
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@seriNo", SqlDbType = SqlDbType.VarChar, Size = 25, Value = seriNo });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@stokKodu", SqlDbType = SqlDbType.VarChar, Size = 25, Value = stokKodu });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@sharId", SqlDbType = SqlDbType.Int, Value = sharId });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@tarih", SqlDbType = SqlDbType.DateTime, Value = DateTime.Now });
                cmd.ExecuteNonQuery();



                cmd.CommandText = sb2.ToString();
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@alisSeri", SqlDbType = SqlDbType.VarChar, Size = 6, Value = alisSeri });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@alisSira", SqlDbType = SqlDbType.Int, Value = alisSira });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@satisSeri", SqlDbType = SqlDbType.VarChar, Size = 6, Value = satisSeri });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@satisSira", SqlDbType = SqlDbType.Int, Value = satisSira });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@alisFiyat", SqlDbType = SqlDbType.Float, Value = alisFiyat });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@satisFiyat", SqlDbType = SqlDbType.Float, Value = satisFiyat });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@alisTarih", SqlDbType = SqlDbType.DateTime, Value = alisTarihi });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@satisTarih", SqlDbType = SqlDbType.DateTime, Value = satisTarihi });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@garantiBaslangic", SqlDbType = SqlDbType.DateTime, Value = garantiBaslangic });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@garantiBitis", SqlDbType = SqlDbType.DateTime, Value = garantiBitis });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@alisCari", SqlDbType = SqlDbType.VarChar, Size = 25, Value = alisCari });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@satisCari", SqlDbType = SqlDbType.VarChar, Size = 25, Value = alisCari });
                cmd.ExecuteNonQuery();
            }
        }


        public byte[] SharEvrakTip(EvrakTipleri evrakTip)
        {
            byte[] sonuc = new byte[4];
            switch (evrakTip)
            {
                case EvrakTipleri.SatisFaturasi:
                    sonuc[0] = 1;
                    sonuc[1] = 0;
                    sonuc[2] = 0;
                    sonuc[3] = 4;
                    break;
                case EvrakTipleri.AlisFaturasi:
                    sonuc[0] = 0;
                    sonuc[1] = 0;
                    sonuc[2] = 0;
                    sonuc[3] = 3;
                    break;
                case EvrakTipleri.AlisIrsaliyesi:
                    sonuc[0] = 0;
                    sonuc[1] = 0;
                    sonuc[2] = 0;
                    sonuc[3] = 13;
                    break;
                case EvrakTipleri.DepoSevk:
                    sonuc[0] = 2;
                    sonuc[1] = 6;
                    sonuc[2] = 0;
                    sonuc[3] = 2;
                    break;
                default:
                    break;
            }
            return sonuc;
        }

        public byte[] CharEvrakTip(EvrakTipleri evrakTip)
        {
            byte[] sonuc = new byte[4];
            switch (evrakTip)
            {
                case EvrakTipleri.SatisFaturasi:
                    sonuc[0] = 0;
                    sonuc[1] = 6;
                    sonuc[2] = 0;
                    sonuc[3] = 63;
                    break;
                case EvrakTipleri.AlisFaturasi:
                    sonuc[0] = 1;
                    sonuc[1] = 6;
                    sonuc[2] = 0;
                    sonuc[3] = 0;
                    break;
                case EvrakTipleri.GelenHavale:
                    sonuc[0] = 1;
                    sonuc[1] = 0;
                    sonuc[2] = 0;
                    sonuc[3] = 34;
                    break;
                case EvrakTipleri.GidenHavale:
                    sonuc[0] = 0;
                    sonuc[1] = 0;
                    sonuc[2] = 0;
                    sonuc[3] = 35;
                    break;
            }
            return sonuc;
        }

        public int MaxEvrakNo(EvrakTipleri tip, String seri)
        {
            byte[] dizi;
            int sira = 1;
            using (SqlConnection cn = new SqlConnection(GetString()))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("", cn))
                {
                    switch (tip)
                    {
                        case EvrakTipleri.SatisFaturasi:
                        case EvrakTipleri.GidenHavale:
                        case EvrakTipleri.GelenHavale:
                        case EvrakTipleri.AlisFaturasi:
                            dizi = CharEvrakTip(tip);
                            cmd.CommandText = string.Format("exec sp_executesql N'SELECT     ISNULL(MAX(cha_evrakno_sira),0) + 1 FROM         CARI_HESAP_HAREKETLERI WITH (NOLOCK) WHERE     (cha_tip = {1}) AND (cha_normal_Iade = {2}) AND (cha_evrak_tip = {3}) AND (cha_evrakno_seri = ''{0}'')'", seri, dizi[0], dizi[2], dizi[3]);
                            break;
                        //case EvrakTipleri.AlinanSiparis:
                        //    cmd.CommandText = string.Format("exec sp_executesql N'SELECT     ISNULL(MAX(sip_evrakno_sira),0) + 1 FROM         SIPARISLER WITH (NOLOCK) WHERE     (sip_tip = 0) AND (sip_cins = 0) AND (sip_evrakno_seri = ''{0}'')'", seri);
                        //    break;
                        //case EvrakTipleri.VerilenSiparis:
                        //    cmd.CommandText = string.Format("exec sp_executesql N'SELECT     ISNULL(MAX(sip_evrakno_sira),0) + 1 FROM         SIPARISLER WITH (NOLOCK) WHERE     (sip_tip = 1) AND (sip_cins = 0) AND (sip_evrakno_seri = ''{0}'')'", seri);
                        //    break;
                        case EvrakTipleri.DepoSevk:
                            dizi = SharEvrakTip(tip);
                            cmd.CommandText = string.Format("exec sp_executesql N'SELECT     ISNULL(MAX(sth_evrakno_sira),0) + 1 as sira  FROM         STOK_HAREKETLERI WITH (NOLOCK, INDEX=NDX_STOK_HAREKETLERI_05) WHERE     (sth_tip = 2) AND (sth_evraktip = 2) AND (sth_evrakno_seri = ''{0}'')'", seri);
                            break;
                        case EvrakTipleri.AlisIrsaliyesi:
                            dizi = SharEvrakTip(tip);
                            cmd.CommandText = string.Format("exec sp_executesql N'SELECT     ISNULL(MAX(sth_evrakno_sira),0) + 1 as sira  FROM         STOK_HAREKETLERI WITH (NOLOCK, INDEX=NDX_STOK_HAREKETLERI_05) WHERE  (sth_evraktip = {1}) AND (sth_evrakno_seri = ''{0}'')'", seri, dizi[3]);
                            break;
                    }

                    sira = Convert.ToInt32(cmd.ExecuteScalar());
                }
                cn.Close();
            }
            return sira;
        }
        private bool CheckEvrakNo(EvrakTipleri tip, String seri, int sira)
        {
            byte[] dizi;
            bool evrakVar = false;
            using (SqlConnection cn = new SqlConnection(GetString()))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("", cn))
                {
                    switch (tip)
                    {
                        case EvrakTipleri.SatisFaturasi:
                        case EvrakTipleri.GidenHavale:
                        case EvrakTipleri.GelenHavale:
                        case EvrakTipleri.AlisFaturasi:
                            dizi = CharEvrakTip(tip);
                            cmd.CommandText = string.Format("exec sp_executesql N'SELECT     COUNT(*) FROM         CARI_HESAP_HAREKETLERI WITH (NOLOCK) WHERE     (cha_tip = {1}) AND (cha_normal_Iade = {2}) AND (cha_evrak_tip = {3}) AND (cha_evrakno_seri = ''{0}'') AND (cha_evrakno_sira={4})'", seri, dizi[0], dizi[2], dizi[3], sira);
                            break;
                        //case EvrakTipleri.AlinanSiparis:
                        //    cmd.CommandText = string.Format("exec sp_executesql N'SELECT     COUNT(*) FROM         SIPARISLER WITH (NOLOCK) WHERE     (sip_tip = 0) AND (sip_cins = 0) AND (sip_evrakno_seri = ''{0}'') AND (sip_evrakno_sira={1})'", seri, sira);
                        //    break;
                        //case EvrakTipleri.VerilenSiparis:
                        //    cmd.CommandText = string.Format("exec sp_executesql N'SELECT     COUNT(*) FROM         SIPARISLER WITH (NOLOCK) WHERE     (sip_tip = 1) AND (sip_cins = 0) AND (sip_evrakno_seri = ''{0}'')  AND (sip_evrakno_sira={1})'", seri, sira);
                        //    break;
                        case EvrakTipleri.DepoSevk:
                            dizi = SharEvrakTip(tip);
                            cmd.CommandText = string.Format("exec sp_executesql N'SELECT     COUNT(*)  FROM         STOK_HAREKETLERI WITH (NOLOCK, INDEX=NDX_STOK_HAREKETLERI_05) WHERE     (sth_tip = 2) AND (sth_evraktip = 2) AND (sth_evrakno_seri = ''{0}'') AND (sth_evrakno_sira={1})'", seri, sira);
                            break;
                        case EvrakTipleri.AlisIrsaliyesi:
                            dizi = SharEvrakTip(tip);
                            cmd.CommandText = string.Format("exec sp_executesql N'SELECT     COUNT(*)  FROM         STOK_HAREKETLERI WITH (NOLOCK, INDEX=NDX_STOK_HAREKETLERI_05) WHERE  (sth_evraktip = {1}) AND (sth_evrakno_seri = ''{0}'') AND (sth_evrakno_sira={2})'", seri, dizi[3], sira);
                            break;
                    }

                    evrakVar = Convert.ToBoolean(cmd.ExecuteScalar());
                }
                cn.Close();
            }
            return evrakVar;
        }
        private Dictionary<int, int> KdvList(string cnnString)
        {
            Dictionary<int, int> sonuc = new Dictionary<int, int>();
            using (SqlConnection cn = new SqlConnection(cnnString))
            {
                cn.Open();
                using (SqlDataAdapter adp = new SqlDataAdapter("select dbo.fn_VergiYuzde(1) as oran union all select dbo.fn_VergiYuzde(2) union all select dbo.fn_VergiYuzde(3) union all select dbo.fn_VergiYuzde(4) union all select dbo.fn_VergiYuzde(5) union all select dbo.fn_VergiYuzde(6)", cn))
                {
                    DataTable dt = new DataTable();
                    adp.Fill(dt);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sonuc.Add(i + 1, Convert.ToInt32(dt.Rows[i][0]));
                    }
                }
                cn.Close();
            }
            return sonuc;
        }
        private bool SharEkle(Evrak evrak, SqlTransaction trn)
        {
            bool sonuc = false;
            StringBuilder sb = new StringBuilder();
            sb.Append(" if exists(SELECT     sth_evrakno_seri  FROM         STOK_HAREKETLERI  WHERE     (sth_evrakno_seri = @Seri) AND (sth_evrakno_sira =@Sira) AND (sth_satirno = @Satir) AND (sth_evraktip = @Evrak_Tip) AND (sth_tip=@Tip) AND (sth_cins=@Cins) and (sth_normal_iade=@Normal_iade)) ");
            sb.Append(" 		UPDATE STOK_HAREKETLERI SET ");
            sb.Append(" 							  sth_lastup_user=@kullanici, sth_lastup_date=@crDate, sth_tarih=@Evrak_Tarih, sth_tip=@Tip, sth_cins=@Cins, sth_normal_iade=@Normal_iade, sth_belge_tarih=@Evrak_Tarih,  ");
            sb.Append(" 							  sth_stok_kod=@Stok_kod, sth_cari_kodu=@Cari_kod, sth_plasiyer_kodu=@plasiyer, sth_kur_tarihi='1899-12-30 00:00:00', sth_miktar=@Miktar, sth_miktar2=@Miktar, sth_birim_pntr=1, sth_tutar=@yekun, sth_iskonto1=@isk1_tutar, sth_iskonto2=@isk2_tutar, sth_iskonto3=@isk3_tutar,  ");
            sb.Append(" 							  sth_iskonto4=@isk4_tutar, sth_iskonto5=@isk5_tutar, sth_vergi_pntr=@Kdv,sth_vergi=@kdv_tutar, sth_odeme_op=@odeme, sth_sip_recid_dbcno=@norm_sip_dbc, sth_sip_recid_recno=@norm_sip_rec,sth_fat_recid_dbcno=0,sth_fat_recid_recno=@fat_rec_no, sth_giris_depo_no=@GirisDepo, sth_cikis_depo_no=@CikisDepo,  ");
            sb.Append(" 							  sth_malkbl_sevk_tarihi=@Evrak_Tarih, sth_cari_srm_merkezi=@srmerkez, sth_stok_srm_merkezi=@srmerkez, sth_fis_tarihi='1899-12-30 00:00:00', sth_maliyet_ana=@Fiyat, sth_maliyet_alternatif=@Fiyat, sth_maliyet_orjinal=@Fiyat, sth_vardiya_tarihi='1899-12-30 00:00:00',sth_subesip_recid_dbcno=@sube_sip_dbc,sth_subesip_recid_recno=@sube_sip_rec,sth_belge_no=@belge_no, ");
            sb.Append(" 							  sth_firmano=@firma_no,sth_subeno=@sube_no,sth_proje_kodu=@proje ");
            sb.Append(" 		WHERE     (sth_evrakno_seri = @Seri) AND (sth_evrakno_sira =@Sira) AND (sth_satirno = @Satir) AND (sth_evraktip = @Evrak_Tip AND (sth_tip=@Tip) AND (sth_cins=@Cins) and (sth_normal_iade=@Normal_iade)) ");
            sb.Append(" else ");
            sb.Append(" 		INSERT INTO STOK_HAREKETLERI ");
            sb.Append(" 		 (sth_RECid_DBCno,sth_RECid_RECno,sth_SpecRECno,sth_iptal,sth_fileid,sth_hidden,sth_kilitli,sth_degisti,sth_checksum,sth_create_user,sth_create_date,sth_lastup_user,sth_lastup_date,sth_special1,sth_special2,sth_special3,sth_firmano,sth_subeno,sth_tarih,sth_tip,sth_cins,sth_normal_iade,sth_evraktip,sth_evrakno_seri,sth_evrakno_sira,sth_satirno,sth_belge_no,sth_belge_tarih,sth_stok_kod,sth_isk_mas1,sth_isk_mas2,sth_isk_mas3,sth_isk_mas4,sth_isk_mas5,sth_isk_mas6,sth_isk_mas7,sth_isk_mas8,sth_isk_mas9,sth_isk_mas10,sth_sat_iskmas1,sth_sat_iskmas2,sth_sat_iskmas3,sth_sat_iskmas4,sth_sat_iskmas5,sth_sat_iskmas6,sth_sat_iskmas7,sth_sat_iskmas8,sth_sat_iskmas9,sth_sat_iskmas10,sth_pos_satis,sth_cari_cinsi,sth_cari_kodu,sth_cari_grup_no,sth_isemri_gider_kodu,sth_ismerkezi_kodu,sth_plasiyer_kodu,sth_kur_tarihi,sth_har_doviz_cinsi,sth_har_doviz_kuru,sth_alt_doviz_kuru,sth_stok_doviz_cinsi,sth_stok_doviz_kuru,sth_miktar,sth_miktar2,sth_birim_pntr,sth_tutar,sth_iskonto1,sth_iskonto2,sth_iskonto3,sth_iskonto4,sth_iskonto5,sth_iskonto6,sth_masraf1,sth_masraf2,sth_masraf3,sth_masraf4,sth_vergi_pntr,sth_vergi,sth_masraf_vergi_pntr,sth_masraf_vergi,sth_netagirlik,sth_odeme_op,sth_aciklama,sth_sip_recid_dbcno,sth_sip_recid_recno,sth_fat_recid_dbcno,sth_fat_recid_recno,sth_giris_depo_no,sth_cikis_depo_no,sth_malkbl_sevk_tarihi,sth_cari_srm_merkezi,sth_stok_srm_merkezi,sth_fis_tarihi,sth_fis_sirano,sth_vergisiz_fl,sth_maliyet_ana,sth_maliyet_alternatif,sth_maliyet_orjinal,sth_adres_no,sth_parti_kodu,sth_lot_no,sth_kons_recid_dbcno,sth_kons_recid_recno,sth_vardiya_tarihi,sth_vardiya_no,sth_satistipi,sth_proje_kodu,sth_ihracat_kredi_kodu,sth_exim_kodu,sth_otv_pntr,sth_otv_vergi,sth_bkm_recid_dbcno,sth_bkm_recid_recno,sth_karsikons_recid_dbcno,sth_karsikons_recid_recno,sth_iade_evrak_seri,sth_iade_evrak_sira,sth_diib_belge_no,sth_diib_satir_no,sth_mensey_ulke_tipi,sth_mensey_ulke_kodu,sth_brutagirlik,sth_halrehmiktari,sth_halrehfiyati,sth_halsandikmiktari,sth_halsandikfiyati,sth_halsandikkdvtutari,sth_disticaret_turu,sth_otvtutari,sth_otvvergisiz_fl,sth_direkt_iscilik_1,sth_direkt_iscilik_2,sth_direkt_iscilik_3,sth_direkt_iscilik_4,sth_direkt_iscilik_5,sth_genel_uretim_1,sth_genel_uretim_2,sth_genel_uretim_3,sth_genel_uretim_4,sth_genel_uretim_5,sth_yat_tes_kodu,sth_oiv_pntr,sth_oiv_vergi,sth_oivvergisiz_fl,sth_subesip_recid_dbcno,sth_subesip_recid_recno) ");
            sb.Append(" 		VALUES (0,-1,0,0,16,0,0,0,0,@kullanici,@crDate,@kullanici,@crDate,'','','',@firma_no,@sube_no,@Evrak_Tarih,@Tip,@Cins,@Normal_iade,@Evrak_Tip,@Seri,@Sira,@Satir,@belge_no,@Evrak_Tarih,@Stok_kod,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,@Cari_kod,0,'','',@plasiyer,'1899-12-30 00:00:00',0,1,1,0,1, @Miktar  , @Miktar  ,1, @yekun   ,@isk1_tutar,@isk2_tutar,@isk3_tutar,@isk4_tutar,@isk5_tutar,0,0,0,0,0,@Kdv,@kdv_tutar,0,0,0,@odeme,'',@norm_sip_dbc,@norm_sip_rec,0,@fat_rec_no,@GirisDepo,@CikisDepo,@Evrak_Tarih,@srmerkez,@srmerkez, '1899-12-30 00:00:00',0,0, @Fiyat  , @Fiyat  , @Fiyat  ,1,'',0,0,0,'1899-12-30 00:00:00',0,0,@proje,'','',0,0,0,0,0,0,'',0,'',0,0,'',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'',0,0,0,@sube_sip_dbc,@sube_sip_rec) ");
            sb.Append(";update STOK_HAREKETLERI set sth_RECid_RECno=sth_RECno where sth_RECid_RECno=-1;;SELECT SCOPE_IDENTITY()");

            DateTime defaultTarih = new DateTime(1899, 12, 31);
            try
            {
                using (SqlCommand cmd = new SqlCommand("", trn.Connection))
                {
                    cmd.Transaction = trn;
                    byte[] tipCins = SharEvrakTip(evrak.EvrakCinsi);
                    cmd.CommandText = sb.ToString();

                    cmd.Parameters.Add("@Tip", SqlDbType.Int);
                    cmd.Parameters.Add("@Cins", SqlDbType.Int);
                    cmd.Parameters.Add("@Normal_iade", SqlDbType.Int);
                    cmd.Parameters.Add("@Evrak_Tip", SqlDbType.Int);
                    cmd.Parameters.Add("@Evrak_Tarih", SqlDbType.DateTime);
                    cmd.Parameters.Add("@Seri", SqlDbType.VarChar);
                    cmd.Parameters.Add("@Sira", SqlDbType.Int);
                    cmd.Parameters.Add("@crDate", SqlDbType.DateTime);
                    cmd.Parameters.Add("@Cari_kod", SqlDbType.VarChar);
                    cmd.Parameters.Add("@plasiyer", SqlDbType.VarChar);
                    cmd.Parameters.Add("@srmerkez", SqlDbType.VarChar);
                    cmd.Parameters.Add("@kullanici", SqlDbType.VarChar);
                    cmd.Parameters.Add("@GirisDepo", SqlDbType.Int);
                    cmd.Parameters.Add("@CikisDepo", SqlDbType.Int);
                    cmd.Parameters.Add("@odeme", SqlDbType.Int);
                    cmd.Parameters.Add("@belge_no", SqlDbType.VarChar);
                    cmd.Parameters.Add("@firma_no", SqlDbType.Int);
                    cmd.Parameters.Add("@sube_no", SqlDbType.Int);
                    cmd.Parameters.Add("@proje", SqlDbType.VarChar);
                    cmd.Parameters.Add("@Satir", SqlDbType.Int);
                    cmd.Parameters.Add("@Stok_kod", SqlDbType.VarChar);
                    cmd.Parameters.Add("@Miktar", SqlDbType.Float);
                    cmd.Parameters.Add("@yekun", SqlDbType.Float);
                    cmd.Parameters.Add("@isk1_tutar", SqlDbType.Float);
                    cmd.Parameters.Add("@isk2_tutar", SqlDbType.Float);
                    cmd.Parameters.Add("@isk3_tutar", SqlDbType.Float);
                    cmd.Parameters.Add("@isk4_tutar", SqlDbType.Float);
                    cmd.Parameters.Add("@isk5_tutar", SqlDbType.Float);
                    cmd.Parameters.Add("@Kdv", SqlDbType.Int);
                    cmd.Parameters.Add("@sube_sip_rec", SqlDbType.Int);
                    cmd.Parameters.Add("@sube_sip_dbc", SqlDbType.Int);
                    cmd.Parameters.Add("@norm_sip_rec", SqlDbType.Int);
                    cmd.Parameters.Add("@norm_sip_dbc", SqlDbType.Int);
                    cmd.Parameters.Add("@Fiyat", SqlDbType.Float);
                    cmd.Parameters.Add("@kdv_tutar", SqlDbType.Float);
                    cmd.Parameters.Add("@fat_rec_no", SqlDbType.Int);

                    foreach (HareketBilgileri row in evrak.Rows)
                    {
                        cmd.Parameters["@Tip"].Value = tipCins[0];
                        cmd.Parameters["@Cins"].Value = tipCins[1];
                        cmd.Parameters["@Normal_iade"].Value = tipCins[2];
                        cmd.Parameters["@Evrak_Tip"].Value = tipCins[3];
                        cmd.Parameters["@Evrak_Tarih"].Value = new DateTime(evrak.Tarih.Year, evrak.Tarih.Month, evrak.Tarih.Day);
                        cmd.Parameters["@Seri"].Value = evrak.Seri;
                        cmd.Parameters["@Sira"].Value = evrak.Sira;
                        cmd.Parameters["@crDate"].Value = DateTime.Now;
                        cmd.Parameters["@Cari_kod"].Value = evrak.CariKodu;
                        cmd.Parameters["@plasiyer"].Value = evrak.PlasiyerKodu;
                        cmd.Parameters["@srmerkez"].Value = evrak.SrmMerkezKodu;
                        cmd.Parameters["@kullanici"].Value = evrak.MikroKullanici;
                        cmd.Parameters["@GirisDepo"].Value = evrak.GirisDepoNo;
                        cmd.Parameters["@CikisDepo"].Value = evrak.CikisDepoNo;
                        cmd.Parameters["@odeme"].Value = evrak.Vade;
                        cmd.Parameters["@belge_no"].Value = evrak.BelgeNo;
                        cmd.Parameters["@firma_no"].Value = 0;
                        cmd.Parameters["@sube_no"].Value = 0;
                        cmd.Parameters["@proje"].Value = "";
                        cmd.Parameters["@Satir"].Value = row.Satir;
                        cmd.Parameters["@Stok_kod"].Value = row.StokKodu;
                        cmd.Parameters["@Miktar"].Value = row.Miktar;
                        cmd.Parameters["@yekun"].Value = row.BurutTutar;
                        cmd.Parameters["@isk1_tutar"].Value = row.Isk1Tutar;
                        cmd.Parameters["@isk2_tutar"].Value = row.Isk2Tutar;
                        cmd.Parameters["@isk3_tutar"].Value = row.Isk3Tutar;
                        cmd.Parameters["@isk4_tutar"].Value = row.Isk4Tutar;
                        cmd.Parameters["@isk5_tutar"].Value = row.Isk5Tutar;
                        cmd.Parameters["@Kdv"].Value = row.KdvSira;

                        if (evrak.EvrakCinsi == EvrakTipleri.DepoSevk)
                        {
                            cmd.Parameters["@sube_sip_rec"].Value = row.SipRecNo;
                            cmd.Parameters["@sube_sip_dbc"].Value = row.SipDbcNo;
                            cmd.Parameters["@norm_sip_rec"].Value = 0;
                            cmd.Parameters["@norm_sip_dbc"].Value = 0;
                        }
                        else
                        {
                            cmd.Parameters["@sube_sip_rec"].Value = 0;
                            cmd.Parameters["@sube_sip_dbc"].Value = 0;
                            cmd.Parameters["@norm_sip_rec"].Value = row.SipRecNo;
                            cmd.Parameters["@norm_sip_dbc"].Value = row.SipDbcNo;
                        }

                        cmd.Parameters["@Fiyat"].Value = row.Fiyat;
                        cmd.Parameters["@kdv_tutar"].Value = row.KdvTutari;
                        cmd.Parameters["@fat_rec_no"].Value = row.FaturaID;

                        object sharid = cmd.ExecuteScalar();
                        if (sharid != null)
                        {
                            int shrid = Cevir.Parse<int>(sharid);

                            if (row.SeriNo != null && row.SeriNo.Length > 0)
                            {
                                DateTime tarih = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                                foreach (string serino in row.SeriNo)
                                {
                                    SerinoHareketEkle(serino, row.StokKodu, shrid, tarih, tarih, tarih, tarih, evrak.Seri, evrak.Sira, evrak.Seri, evrak.Sira, row.Fiyat, row.Fiyat, evrak.CariKodu, evrak.CariKodu, trn);
                                }
                            }
                        }

                    }
                }
                sonuc = true;
            }
            catch (Exception)
            {
            }
            return sonuc;
        }
        private int CharEkle(EvrakTipleri tip, string evrakSeri, int evrakSira, int satir, DateTime evrakTarihi, double meblag, double[] vergiler, string saticiKodu, string cariKodu, double araToplam, string belgeNo, int vade, string aciklama, double isk1Tutar, double isk2Tutar, double isk3Tutar, int cariCins, string kasaKodu, string srmMerkezi, string projeKodu, int kdvSira, SqlTransaction trn)
        {
            string sqlStr = "if exists(SELECT     cha_RECno FROM         CARI_HESAP_HAREKETLERI WHERE     (cha_evrak_tip = @cha_evrak_tip) AND (cha_evrakno_seri = @cha_evrakno_seri) AND (cha_evrakno_sira = @cha_evrakno_sira) AND (cha_satir_no = @cha_satir_no)) delete FROM CARI_HESAP_HAREKETLERI WHERE     (cha_evrak_tip = @cha_evrak_tip) AND (cha_evrakno_seri = @cha_evrakno_seri) AND (cha_evrakno_sira = @cha_evrakno_sira) AND (cha_satir_no = @cha_satir_no);insert into CARI_HESAP_HAREKETLERI (cha_RECid_DBCno,cha_RECid_RECno,cha_SpecRecNo,cha_iptal,cha_fileid,cha_hidden,cha_kilitli,cha_degisti,cha_CheckSum,cha_create_user,cha_create_date,cha_lastup_user,cha_lastup_date,cha_special1,cha_special2,cha_special3,cha_firmano,cha_subeno,cha_tarihi,cha_tip,cha_cinsi,cha_normal_Iade,cha_evrak_tip,cha_satir_no,cha_evrakno_seri,cha_evrakno_sira,cha_belge_no,cha_belge_tarih,cha_cari_cins,cha_kod,cha_kasa_hizmet,cha_kasa_hizkod,cha_d_kurtar,cha_d_cins,cha_d_kur,cha_altd_kur,cha_grupno,cha_meblag,cha_vade,cha_fis_tarih,cha_fis_sirano,cha_ft_iskonto1,cha_ft_iskonto2,cha_ft_iskonto3,cha_ft_iskonto4,cha_ft_iskonto5,cha_ft_iskonto6,cha_ft_masraf1,cha_ft_masraf2,cha_ft_masraf3,cha_ft_masraf4,cha_vergi1,cha_vergi2,cha_vergi3,cha_vergi4,cha_vergi5,cha_vergi6,cha_vergi7,cha_vergi8,cha_vergi9,cha_vergi10,cha_yuvarlama,cha_tpoz,cha_aciklama,cha_trefno,cha_sntck_poz,cha_karsidcinsi,cha_karsid_kur,cha_karsidgrupno,cha_srmrkkodu,cha_reftarihi,cha_odeme_arr1,cha_odeme_arr2,cha_odeme_arr3,cha_odeme_arr4,cha_odeme_arr5,cha_odeme_arr6,cha_odeme_arr7,cha_odeme_arr8,cha_miktari,cha_aratoplam,cha_vergipntr,cha_istisnakodu,cha_ver_tev_carpani,cha_stopaj,cha_savsandesfonu,cha_vergisiz_fl,cha_satici_kodu,cha_mustahsil_borsa,cha_mustahsil_bagkur,cha_mustahsil_diger,cha_HalMSDF,cha_HalHamaliye,cha_HalStopaj,cha_HalKomisyonu,cha_StFonPntr,cha_pos_hareketi,cha_vardiya_tarihi,cha_vardiya_no,cha_vardiya_evrak_ti,cha_HalRusum,cha_HalNavlunTut,cha_HalRehinFuture,cha_HalKomisyon,cha_Vade_Farki_Yuz,cha_karsisrmrkkodu,cha_EXIMkodu,cha_HalRehinSandikmiktari,cha_HalSandikVrMiktar,cha_HalSandikTutari,cha_HalSandikKDVTutari,cha_HalrehinSandikTutari,cha_Tevkifat_turu,cha_ticaret_turu,cha_otvtutari,cha_otvvergisiz_fl,cha_projekodu,cha_sozlesme_DBCno,cha_sozlesme_RECno,cha_yat_tes_kodu,cha_ciro_cari_kodu,cha_oivergisiz_fl,cha_meblag_ana_doviz_icin_gecersiz_fl,cha_meblag_alt_doviz_icin_gecersiz_fl,cha_meblag_orj_doviz_icin_gecersiz_fl,cha_ciroprim_DBCno,cha_ciroprim_RECno,cha_HalHamaliyeKdv,cha_HalHamaliyeVergisiz_fl) values (@cha_RECid_DBCno,@cha_RECid_RECno,@cha_SpecRecNo,@cha_iptal,@cha_fileid,@cha_hidden,@cha_kilitli,@cha_degisti,@cha_CheckSum,@cha_create_user,@cha_create_date,@cha_lastup_user,@cha_lastup_date,@cha_special1,@cha_special2,@cha_special3,@cha_firmano,@cha_subeno,@cha_tarihi,@cha_tip,@cha_cinsi,@cha_normal_Iade,@cha_evrak_tip,@cha_satir_no,@cha_evrakno_seri,@cha_evrakno_sira,@cha_belge_no,@cha_belge_tarih,@cha_cari_cins,@cha_kod,@cha_kasa_hizmet,@cha_kasa_hizkod,@cha_d_kurtar,@cha_d_cins,@cha_d_kur,@cha_altd_kur,@cha_grupno,@cha_meblag,@cha_vade,@cha_fis_tarih,@cha_fis_sirano,@cha_ft_iskonto1,@cha_ft_iskonto2,@cha_ft_iskonto3,@cha_ft_iskonto4,@cha_ft_iskonto5,@cha_ft_iskonto6,@cha_ft_masraf1,@cha_ft_masraf2,@cha_ft_masraf3,@cha_ft_masraf4,@cha_vergi1,@cha_vergi2,@cha_vergi3,@cha_vergi4,@cha_vergi5,@cha_vergi6,@cha_vergi7,@cha_vergi8,@cha_vergi9,@cha_vergi10,@cha_yuvarlama,@cha_tpoz,@cha_aciklama,@cha_trefno,@cha_sntck_poz,@cha_karsidcinsi,@cha_karsid_kur,@cha_karsidgrupno,@cha_srmrkkodu,@cha_reftarihi,@cha_odeme_arr1,@cha_odeme_arr2,@cha_odeme_arr3,@cha_odeme_arr4,@cha_odeme_arr5,@cha_odeme_arr6,@cha_odeme_arr7,@cha_odeme_arr8,@cha_miktari,@cha_aratoplam,@cha_vergipntr,@cha_istisnakodu,@cha_ver_tev_carpani,@cha_stopaj,@cha_savsandesfonu,@cha_vergisiz_fl,@cha_satici_kodu,@cha_mustahsil_borsa,@cha_mustahsil_bagkur,@cha_mustahsil_diger,@cha_HalMSDF,@cha_HalHamaliye,@cha_HalStopaj,@cha_HalKomisyonu,@cha_StFonPntr,@cha_pos_hareketi,@cha_vardiya_tarihi,@cha_vardiya_no,@cha_vardiya_evrak_ti,@cha_HalRusum,@cha_HalNavlunTut,@cha_HalRehinFuture,@cha_HalKomisyon,@cha_Vade_Farki_Yuz,@cha_karsisrmrkkodu,@cha_EXIMkodu,@cha_HalRehinSandikmiktari,@cha_HalSandikVrMiktar,@cha_HalSandikTutari,@cha_HalSandikKDVTutari,@cha_HalrehinSandikTutari,@cha_Tevkifat_turu,@cha_ticaret_turu,@cha_otvtutari,@cha_otvvergisiz_fl,@cha_projekodu,@cha_sozlesme_DBCno,@cha_sozlesme_RECno,@cha_yat_tes_kodu,@cha_ciro_cari_kodu,@cha_oivergisiz_fl,@cha_meblag_ana_doviz_icin_gecersiz_fl,@cha_meblag_alt_doviz_icin_gecersiz_fl,@cha_meblag_orj_doviz_icin_gecersiz_fl,@cha_ciroprim_DBCno,@cha_ciroprim_RECno,@cha_HalHamaliyeKdv,@cha_HalHamaliyeVergisiz_fl);SELECT SCOPE_IDENTITY()";
            List<SqlParameter> prmList = new List<SqlParameter>();
            byte[] tipler = CharEvrakTip(tip);

            prmList.Add(new SqlParameter() { ParameterName = "@cha_RECid_DBCno", SqlDbType = SqlDbType.SmallInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_RECid_RECno", SqlDbType = SqlDbType.Int, Value = -1 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_SpecRecNo", SqlDbType = SqlDbType.Int, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_iptal", SqlDbType = SqlDbType.Bit, Value = false });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_fileid", SqlDbType = SqlDbType.SmallInt, Value = 51 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_hidden", SqlDbType = SqlDbType.Bit, Value = false });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_kilitli", SqlDbType = SqlDbType.Bit, Value = false });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_degisti", SqlDbType = SqlDbType.Bit, Value = false });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_CheckSum", SqlDbType = SqlDbType.Int, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_create_user", SqlDbType = SqlDbType.SmallInt, Value = 1 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_create_date", SqlDbType = SqlDbType.DateTime, Value = evrakTarihi });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_lastup_user", SqlDbType = SqlDbType.SmallInt, Value = 1 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_lastup_date", SqlDbType = SqlDbType.DateTime, Value = evrakTarihi });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_special1", SqlDbType = SqlDbType.NVarChar, Value = "" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_special2", SqlDbType = SqlDbType.NVarChar, Value = "" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_special3", SqlDbType = SqlDbType.NVarChar, Value = "" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_firmano", SqlDbType = SqlDbType.Int, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_subeno", SqlDbType = SqlDbType.Int, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_tarihi", SqlDbType = SqlDbType.DateTime, Value = evrakTarihi });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_tip", SqlDbType = SqlDbType.TinyInt, Value = tipler[0] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_cinsi", SqlDbType = SqlDbType.TinyInt, Value = tipler[1] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_normal_Iade", SqlDbType = SqlDbType.TinyInt, Value = tipler[2] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_evrak_tip", SqlDbType = SqlDbType.TinyInt, Value = tipler[3] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_satir_no", SqlDbType = SqlDbType.Int, Value = satir });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_evrakno_seri", SqlDbType = SqlDbType.NVarChar, Value = evrakSeri });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_evrakno_sira", SqlDbType = SqlDbType.Int, Value = evrakSira });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_belge_no", SqlDbType = SqlDbType.NVarChar, Value = belgeNo });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_belge_tarih", SqlDbType = SqlDbType.DateTime, Value = evrakTarihi });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_cari_cins", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_kod", SqlDbType = SqlDbType.NVarChar, Value = cariKodu });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_kasa_hizmet", SqlDbType = SqlDbType.TinyInt, Value = 3 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_kasa_hizkod", SqlDbType = SqlDbType.NVarChar, Value = kasaKodu });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_d_kurtar", SqlDbType = SqlDbType.DateTime, Value = "1899-12-30 00:00:00" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_d_cins", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_d_kur", SqlDbType = SqlDbType.Float, Value = 1 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_altd_kur", SqlDbType = SqlDbType.Float, Value = 1 });

            if (cariCins == 2)
            {
                prmList.Add(new SqlParameter() { ParameterName = "@cha_grupno", SqlDbType = SqlDbType.TinyInt, Value = 1 });
            }
            else
            {
                prmList.Add(new SqlParameter() { ParameterName = "@cha_grupno", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            }

            prmList.Add(new SqlParameter() { ParameterName = "@cha_meblag", SqlDbType = SqlDbType.Float, Value = meblag });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vade", SqlDbType = SqlDbType.Int, Value = vade });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_fis_tarih", SqlDbType = SqlDbType.DateTime, Value = "1899-12-30 00:00:00" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_fis_sirano", SqlDbType = SqlDbType.Int, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_iskonto1", SqlDbType = SqlDbType.Float, Value = isk1Tutar });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_iskonto2", SqlDbType = SqlDbType.Float, Value = isk2Tutar });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_iskonto3", SqlDbType = SqlDbType.Float, Value = isk3Tutar });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_iskonto4", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_iskonto5", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_iskonto6", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_masraf1", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_masraf2", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_masraf3", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ft_masraf4", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi1", SqlDbType = SqlDbType.Float, Value = vergiler[0] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi2", SqlDbType = SqlDbType.Float, Value = vergiler[1] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi3", SqlDbType = SqlDbType.Float, Value = vergiler[2] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi4", SqlDbType = SqlDbType.Float, Value = vergiler[3] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi5", SqlDbType = SqlDbType.Float, Value = vergiler[4] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi6", SqlDbType = SqlDbType.Float, Value = vergiler[5] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi7", SqlDbType = SqlDbType.Float, Value = vergiler[6] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi8", SqlDbType = SqlDbType.Float, Value = vergiler[7] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi9", SqlDbType = SqlDbType.Float, Value = vergiler[8] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergi10", SqlDbType = SqlDbType.Float, Value = vergiler[9] });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_yuvarlama", SqlDbType = SqlDbType.Float, Value = 0 });

            if (cariCins == 4)
                prmList.Add(new SqlParameter() { ParameterName = "@cha_tpoz", SqlDbType = SqlDbType.TinyInt, Value = 1 });
            else
                prmList.Add(new SqlParameter() { ParameterName = "@cha_tpoz", SqlDbType = SqlDbType.TinyInt, Value = 0 });

            prmList.Add(new SqlParameter() { ParameterName = "@cha_aciklama", SqlDbType = SqlDbType.NVarChar, Value = aciklama });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_trefno", SqlDbType = SqlDbType.NVarChar, Value = "" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_sntck_poz", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_karsidcinsi", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_karsid_kur", SqlDbType = SqlDbType.Float, Value = 1 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_karsidgrupno", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_srmrkkodu", SqlDbType = SqlDbType.NVarChar, Value = srmMerkezi });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_reftarihi", SqlDbType = SqlDbType.DateTime, Value = "1899-12-30 00:00:00" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_odeme_arr1", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_odeme_arr2", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_odeme_arr3", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_odeme_arr4", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_odeme_arr5", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_odeme_arr6", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_odeme_arr7", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_odeme_arr8", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_miktari", SqlDbType = SqlDbType.Float, Value = 1 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_aratoplam", SqlDbType = SqlDbType.Float, Value = araToplam });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergipntr", SqlDbType = SqlDbType.TinyInt, Value = kdvSira });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_istisnakodu", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ver_tev_carpani", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_stopaj", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_savsandesfonu", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vergisiz_fl", SqlDbType = SqlDbType.Bit, Value = false });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_satici_kodu", SqlDbType = SqlDbType.NVarChar, Value = saticiKodu });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_mustahsil_borsa", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_mustahsil_bagkur", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_mustahsil_diger", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalMSDF", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalHamaliye", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalStopaj", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalKomisyonu", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_StFonPntr", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_pos_hareketi", SqlDbType = SqlDbType.Bit, Value = false });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vardiya_tarihi", SqlDbType = SqlDbType.DateTime, Value = "1899-12-30 00:00:00" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vardiya_no", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_vardiya_evrak_ti", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalRusum", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalNavlunTut", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalRehinFuture", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalKomisyon", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_Vade_Farki_Yuz", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_karsisrmrkkodu", SqlDbType = SqlDbType.NVarChar, Value = "" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_EXIMkodu", SqlDbType = SqlDbType.NVarChar, Value = "" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalRehinSandikmiktari", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalSandikVrMiktar", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalSandikTutari", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalSandikKDVTutari", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalrehinSandikTutari", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_Tevkifat_turu", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ticaret_turu", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_otvtutari", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_otvvergisiz_fl", SqlDbType = SqlDbType.Bit, Value = false });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_projekodu", SqlDbType = SqlDbType.NVarChar, Value = projeKodu });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_sozlesme_DBCno", SqlDbType = SqlDbType.SmallInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_sozlesme_RECno", SqlDbType = SqlDbType.Int, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_yat_tes_kodu", SqlDbType = SqlDbType.NVarChar, Value = "" });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ciro_cari_kodu", SqlDbType = SqlDbType.NVarChar, Value = cariKodu });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_oivergisiz_fl", SqlDbType = SqlDbType.Bit, Value = false });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_meblag_ana_doviz_icin_gecersiz_fl", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_meblag_alt_doviz_icin_gecersiz_fl", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_meblag_orj_doviz_icin_gecersiz_fl", SqlDbType = SqlDbType.TinyInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ciroprim_DBCno", SqlDbType = SqlDbType.SmallInt, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_ciroprim_RECno", SqlDbType = SqlDbType.Int, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalHamaliyeKdv", SqlDbType = SqlDbType.Float, Value = 0 });
            prmList.Add(new SqlParameter() { ParameterName = "@cha_HalHamaliyeVergisiz_fl", SqlDbType = SqlDbType.Bit, Value = false });

            int sonuc = -1;

            using (SqlCommand cmd = new SqlCommand(sqlStr, trn.Connection))
            {
                cmd.Transaction = trn;
                foreach (var item in prmList)
                {
                    cmd.Parameters.Add(item);
                }
                object recid = cmd.ExecuteScalar();
                if (recid != null)
                {
                    cmd.Parameters.Clear();
                    cmd.CommandText = string.Format("UPDATE CARI_HESAP_HAREKETLERI SET cha_RECid_RECno=cha_RECno WHERE cha_RECno={0}", recid.ToString());
                    cmd.ExecuteNonQuery();
                    sonuc = Convert.ToInt32(recid);
                }
            }
            return sonuc;
        }

        private void SiparisKarsila(int sipID, double miktar, SqlTransaction trn)
        {
            using (SqlCommand cmd = new SqlCommand("UPDATE SIPARISLER SET sip_teslim_miktar = sip_teslim_miktar + @miktar WHERE     (sip_RECno = @id)", trn.Connection))
            {
                cmd.Transaction = trn;
                cmd.Parameters.Add("@id", SqlDbType.Int).Value = sipID;
                cmd.Parameters.Add("@miktar", SqlDbType.Float).Value = miktar;
                cmd.ExecuteNonQuery();
            }
        }
        public bool EvrakEkle(Evrak evrak)
        {
            bool sonuc = false;
            using (SqlConnection cn = new SqlConnection(GetString()))
            {
                cn.Open();
                SqlTransaction trn = cn.BeginTransaction(IsolationLevel.ReadUncommitted);
                double[] vergiTutarlari = new double[10]; ;
                double meblag = 0;
                double aratoplam = 0;
                int satir = 0;
                double isk1 = 0;
                double isk2 = 0;
                double isk3 = 0;
                double isk4 = 0;
                double isk5 = 0;
                foreach (HareketBilgileri item in evrak.Rows)
                {
                    aratoplam += item.BurutTutar;
                    meblag += (item.BurutTutar - (item.Isk1Tutar + item.Isk2Tutar + item.Isk3Tutar + item.Isk4Tutar + item.Isk5Tutar) + item.KdvTutari);
                    vergiTutarlari[item.KdvSira] += item.KdvTutari;
                    isk1 += item.Isk1Tutar;
                    isk2 += item.Isk2Tutar;
                    isk3 += item.Isk3Tutar;
                    isk4 += item.Isk4Tutar;
                    isk5 += item.Isk5Tutar;
                    SiparisKarsila(item.SipRecNo, item.Miktar, trn);
                }

                int recID = CharEkle(evrak.EvrakCinsi, evrak.Seri, evrak.Sira, satir, evrak.Tarih, meblag, vergiTutarlari, evrak.PlasiyerKodu, evrak.CariKodu, aratoplam, evrak.BelgeNo, evrak.Vade, "", isk1, isk2, isk3, 3, "", evrak.SrmMerkezKodu, "", 0, trn);

                foreach (HareketBilgileri item in evrak.Rows)
                {
                    item.FaturaID = recID;
                    SharEkle(evrak, trn);
                }

                try
                {
                    trn.Commit();
                    cn.Close();
                    sonuc = true;
                }
                catch (Exception)
                {
                    if (trn != null)
                        trn.Rollback();
                }
                finally
                {
                    if (cn != null && cn.State == ConnectionState.Open)
                        cn.Close();
                }
            }
            return sonuc;
        }

        public List<ComboItem> DepoList(byte tip)
        {
            List<ComboItem> items = new List<ComboItem>();
            using (SqlConnection cn = new SqlConnection(GetString()))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("", cn))
                {
                    if (tip == 0)
                    {
                        cmd.CommandText = "SELECT  dep_no, dep_adi FROM         DEPOLAR";
                    }
                    else
                    {
                        cmd.CommandText = "SELECT     som_kod, som_isim FROM         SORUMLULUK_MERKEZLERI";
                    }

                    SqlDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                        if (tip == 0)
                        {
                            items.Add(new ComboItem() { Adi = rd.GetString(1), No = rd.GetInt32(0).ToString() });
                        }
                        else
                        {
                            items.Add(new ComboItem() { Adi = rd.GetString(1), No = rd.GetString(0) });
                        }
                    }
                }
                cn.Close();
            }
            return items;
        }
    }
}
